package client.comm.baoding

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import client.comm.commlib.App
import client.comm.commlib.ContextProvider
import client.comm.commlib.SimpleLifecycleCallbacks
import client.comm.commlib.base.FinishReciver
import client.comm.commlib.utils.SharedUtil
import client.comm.baoding.ui.LoginActivity
import com.alipay.sdk.app.APayEntranceActivity
import com.alipay.sdk.app.H5PayActivity
import com.alipay.sdk.app.PayResultActivity
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils

class MyApp : App() {

    companion object {
        const val TAG = "logbd"
    }


    override fun onCreate() {
        super.onCreate()
        initConfig()
    }

    private fun initConfig() {
        LogUtils.getConfig().setGlobalTag("blankj")
        registerLifecycle()
    }

    private fun registerLifecycle() {
        registerActivityLifecycleCallbacks(object : SimpleLifecycleCallbacks() {

            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {

                if (activity is APayEntranceActivity) {
                    ContextProvider.get().attachPay(activity, {
                        SharedUtil.clear("token")
                        it?.let {
                            it.startActivity(Intent(it, LoginActivity::class.java))
                            activity.sendBroadcast(Intent(FinishReciver.FINISH_ACTION))
                        }
                    })

                } else {
                    ContextProvider.get().attach(activity as AppCompatActivity, {
                        SharedUtil.clear("token")
                        it?.let {
                            it.startActivity(Intent(it, LoginActivity::class.java))
                            activity.sendBroadcast(Intent(FinishReciver.FINISH_ACTION))
                        }
                    })
                }


            }

            override fun onActivityResumed(activity: Activity) {
                Log.d("MyApp", activity.localClassName)

                if (activity is APayEntranceActivity) {
                    ContextProvider.get().attachPay(activity, {
                        SharedUtil.clear("token")
                        it?.let {
                            it.startActivity(Intent(it, LoginActivity::class.java))
                            activity.sendBroadcast(Intent(FinishReciver.FINISH_ACTION))
                        }
                    })
                } else {
                    ContextProvider.get().attach(activity as AppCompatActivity, {
                        SharedUtil.clear("token")
                        it?.let {
                            it.startActivity(Intent(it, LoginActivity::class.java))
                            activity.sendBroadcast(Intent(FinishReciver.FINISH_ACTION))
                        }
                    })
                }


            }

            override fun onActivityStopped(activity: Activity) {}

            override fun onActivityDestroyed(activity: Activity) {
                //                ContextProvider.get().detach()
            }
        })
    }


}